package Java8实战._1_Lambda._5_定义接口对选择建模;


import Java8实战._1_Lambda.Apple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _5_定义接口对选择建模 {

    public static void main(String[] args) {

        List<Apple> inventory = Arrays.asList(
                new Apple("red",150),
                new Apple("green",123),
                new Apple("green",189)
        );

        List<Apple> result;

        result = filterApples(inventory, new AppleGreenColorPredicate());
        printAppleList(result);

        System.out.println("\n==========分割线===============\n");

        result = filterApples(inventory, new AppleHeavyWeightPredicate());
        printAppleList(result);

        System.out.println("\n==========分割线===============\n");

        result = filterApples(inventory, new AppleRedAndHeavyPredicate());
        printAppleList(result);

        System.out.println("\n==========分割线===============\n");

        prettyPrintApple(inventory, new AppleFancyFormatter());

        System.out.println("\n==========分割线===============\n");

        prettyPrintApple(inventory, new AppleSimpleFormatter());
    }

    public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate applePredicate){

        List<Apple> list = new ArrayList<>();
        for (Apple apple : inventory){
            if(applePredicate.test(apple)){
                list.add(apple);
            }
        }
        return list;
    }

    public static void printAppleList(List<Apple> list){
        for (Apple apple : list){
            System.out.println("苹果的颜色：" + apple.getColor() + "，苹果的重量：" + apple.getWeight());
        }
    }

    public static void prettyPrintApple(List<Apple> list, AppleFormatter appleFormatter){

        for (Apple apple : list){
            System.out.println(appleFormatter.accept(apple));
        }
    }
}
